/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.OMFActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.LibreGeoSocial.App.R;
import com.LibreGeoSocial.App.OMF.OMFManager;
import com.LibreGeoSocial.App.OMF.SMSSender;

public class SMS extends Activity{
	
	private EditText number;
	
	private static final int DIALOG_WELCOME = 1;
	private static final int COPIAGENDA_OK = 1;
	private static final String MOBILE_PHONE = "phone";
	
	private static final int MENU_COPIAGENDA = Menu.FIRST + 1;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        setContentView(R.layout.sms);
        
        number = (EditText) this.findViewById(R.id.txtDestNum);
        
        Button bsend = (Button)this.findViewById(R.id.btSendSMS);
        
        bsend.setOnClickListener(bsendlistener);
        
        ImageView movistar = (ImageView) findViewById(R.id.imgSMSLogo);
		movistar.setImageResource(R.drawable.combo);
        
        showDialog(DIALOG_WELCOME);
        
    }
	
	OnClickListener bsendlistener = new OnClickListener(){
    	public void onClick(View v){
    		String dnumber = number.getText().toString();
    		
    		/*
    	    SharedPreferences sharedPreferences = 
    	    	PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    	    
        	String unumber = sharedPreferences.getString(LGS_Preferences.KEY_MOVISTAR_USER, "");
        	String upwd = sharedPreferences.getString(LGS_Preferences.KEY_MOVISTAR_PASSWORD, "");
        	*/
    		
    		String unumber = OMFManager.getPhone();
    		String upwd = OMFManager.getMovPwd();
        	
    		
    		if(unumber==null || upwd==null){
    			Toast.makeText(getBaseContext(), 
                        "Please, insert data in main Movistar tab.", 
                        Toast.LENGTH_LONG).show();
    			return;
    		}
    		
    		if(unumber.equals("") || upwd.equals("")){
    			Toast.makeText(getBaseContext(), 
                        "Please, insert data in main Movistar tab", 
                        Toast.LENGTH_LONG).show();
    			return;
    		}
    		
    		if(dnumber.equals("")){
    			Toast.makeText(getBaseContext(), 
                        "Please, insert destination number", 
                        Toast.LENGTH_LONG).show();
    			return;
    		}
    		
    		EditText message = (EditText) findViewById(R.id.txtMessage_SMS);
    		String umessage = message.getText().toString();
    		
    		SMSSender smsAPI = new SMSSender();
    		if(smsAPI.SendMessage(unumber, upwd, dnumber, umessage).equals("OK")){
    			Toast.makeText(getBaseContext(), 
                        "SMS sent", 
                        Toast.LENGTH_LONG).show();
    			finish();
    		}else{
    			Toast.makeText(getBaseContext(), 
                        "SMS couldn't be sent", 
                        Toast.LENGTH_LONG).show();
    		}
    		
    	}
    };
	
    @Override
    protected Dialog onCreateDialog(int id) {
        
    	switch (id) {
	    	case DIALOG_WELCOME:
	    		
	    		LayoutInflater factory1 = LayoutInflater.from(this);
	    		final View textEntryView1 = factory1.inflate(R.layout.sms_welcome, null);
	    		
	    		ImageView logo = (ImageView)textEntryView1.findViewById(R.id.imgPop_up1Logo);
	    		logo.setImageResource(R.drawable.combo);
	    		
		        return new AlertDialog.Builder(this)	        
		        .setTitle("Welcome!")
		        .setView(textEntryView1)
		        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int whichButton) {
		            	
		            }
		        })
		        .create();
    	}
    	
    	return null;
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_COPIAGENDA, 0, "Find number")
        		.setIcon(R.drawable.movistar)
                .setAlphabeticShortcut('C');
                
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	
    	switch (item.getItemId()) {
    	
    	case MENU_COPIAGENDA:
        	
    		Intent i = new Intent(this,PhonesList.class);
        	startActivityForResult(i, COPIAGENDA_OK); 
        	
    		break;
    		
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
    	super.onActivityResult(requestCode, resultCode, data);
    	switch(requestCode){
    	
    	case COPIAGENDA_OK:
    		if(resultCode == Activity.RESULT_OK){
    			number.setText(data.getStringExtra(MOBILE_PHONE));
    			Toast.makeText(getBaseContext(), 
                        "Mobile phone number updated", 
                        Toast.LENGTH_LONG).show();
    		}else{
    			Toast.makeText(getBaseContext(), 
                        "Mobile phone number not updated", 
                        Toast.LENGTH_LONG).show();
    		}
    		break;
    	default:
    		break;
    	}
    }
    
}